package video.zerocopy;

import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

public class NewClient {

    public static void main(String[] args) throws Exception {

        SocketChannel socketChannel = SocketChannel.open();

        socketChannel.connect(new InetSocketAddress(8899));
        socketChannel.configureBlocking(true);

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 1024 * 500);
        long startTime = System.currentTimeMillis();

        FileInputStream fileInputStream = new FileInputStream("C:\\Users\\58212\\Videos\\netty\\53_Netty对Executor的实现机制源码分析.flv");
        FileChannel fileChannel = fileInputStream.getChannel();

        fileChannel.read(byteBuffer);

        long index = 0;
        while (true) {
            long length = fileChannel.transferTo(index, fileChannel.size(), socketChannel);
            index+=length;
            if (length == 0)
                break;
        }

        System.out.println("发送大小："+index+"\t总耗时："+(System.currentTimeMillis()-startTime));

        fileChannel.close();
        fileInputStream.close();
        socketChannel.close();

    }

}
